package com.guowen.myaccount.utils;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.guowen.myaccount.AccManagementActivity;
import com.guowen.myaccount.MainActivity;
import com.guowen.myaccount.R;
import com.guowen.myaccount.adapter.AccManagementAdapter;
import com.guowen.myaccount.db.AccManageBean;
import com.guowen.myaccount.db.DBManager;

import java.util.Calendar;
import java.util.List;
import java.util.zip.Inflater;

public class AccountSettings extends AppCompatActivity implements View.OnClickListener{

    private AccManageBean bean;
    private EditText nameEt, moneyEt, beizhuEt;
    private TextView saveTv, timeTv;
    private ImageView backIv;
    // type 1为修改 0为添加
    private int type, position;
    private List<AccManageBean> mDatas;
    private int year, month, day;

    public interface OnEnsureListener{
        public void onEnsure(String name, String beizhu, float money, int year, int month, int day);
    }

    OnEnsureListener onEnsureListener;

    public void setOnEnsureListener(OnEnsureListener onEnsureListener){
        this.onEnsureListener = onEnsureListener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.settings_account);

        initTime();
        initView();

    }

    private void initView() {
        backIv = findViewById(R.id.settings_account_back);
        nameEt = findViewById(R.id.settings_account_name);
        moneyEt = findViewById(R.id.settings_account_money);
        beizhuEt = findViewById(R.id.settings_account_beizhu);
        saveTv = findViewById(R.id.settings_account_save);
        timeTv = findViewById(R.id.settings_account_time);
        mDatas = DBManager.getAccManList();

        timeTv.setText(year+"-"+month+"-"+day);

        type = getIntent().getIntExtra("type",0);
        if (type == 1){
            position = getIntent().getIntExtra("position",0);
            bean = mDatas.get(position);
            nameEt.setText(bean.getTypename());
            moneyEt.setText(bean.getMoney()+"");
            beizhuEt.setText(bean.getBeizhu());
            year = bean.getYear();
            month = bean.getMonth();
            day = bean.getDay();
            timeTv.setText(year+"-"+month+"-"+day);
        }

        backIv.setOnClickListener(this);
        saveTv.setOnClickListener(this);
        timeTv.setOnClickListener(this);
    }

    //获取今日的具体时间
    private void initTime() {
        Calendar calendar = Calendar.getInstance();
        year = calendar.get(Calendar.YEAR);
        month = calendar.get(Calendar.MONTH) + 1;
        day = calendar.get(Calendar.DAY_OF_MONTH);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.settings_account_back:
                finish();
                break;
            case R.id.settings_account_save:
                if (type == 1){
                    bean.setTypename(nameEt.getText().toString());
                    bean.setBeizhu(beizhuEt.getText().toString());
                    bean.setMoney(Float.parseFloat(moneyEt.getText().toString()));
                    bean.setYear(year);
                    bean.setMonth(month);
                    bean.setDay(day);
                    DBManager.alterItemFromAccmanagetbById(bean.getId(),bean);

                }else{
                    AccManageBean new_bean = new AccManageBean();
                    String name = nameEt.getText().toString();
                    if (name != ""){
                        new_bean.setTypename(name);
                        new_bean.setBeizhu(beizhuEt.getText().toString());
                        String moneyStr = moneyEt.getText().toString();
                        Log.d("moneyStr",moneyStr);
                        if (!moneyStr.isEmpty()){
                            new_bean.setMoney(Float.parseFloat(moneyStr));
                        }else new_bean.setMoney(0);
                        new_bean.setYear(year);
                        new_bean.setMonth(month);
                        new_bean.setDay(day);
                        DBManager.insertItemToAccManagetb(new_bean);
                    }
                }
                AccManagementActivity.sumTv.setText(DBManager.getSumMoneyAll()+"");
                mDatas = DBManager.getAccManList();
                AccManagementActivity.adapter = new AccManagementAdapter(AccManagementActivity.context,mDatas);
                AccManagementActivity.accManageLv.setAdapter(AccManagementActivity.adapter);
                AccManagementActivity.adapter.notifyDataSetChanged();
                finish();
                break;
            case R.id.settings_account_time:
                SelectTimeDialog timeDialog = new SelectTimeDialog(this);
                timeDialog.show();
                timeDialog.setOnEnsureListener(new SelectTimeDialog.OnEnsureListener() {
                    @Override
                    public void onEnsure(String time, int year1, int month1, int day1) {
                        timeTv.setText(year1+"-"+month1+"-"+day1);
                        year = year1;
                        month = month1;
                        day = day1;
                    }
                });
                break;
        }
    }
}
